package clt.com.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import clt.com.mapper.ProductMapper;
import clt.com.object.Product;
import clt.com.service.IProductService;
import clt.com.service.common.impl.BaseServiceImpl;
import clt.com.util.StringUtil;
@Service("productService")
public class ProductServiceImpl extends BaseServiceImpl implements IProductService{
	@Autowired
    private ProductMapper productMapper;
	/**
	 * 
	 * @Title  产品介绍查询
	 * @Description:  queryProductList
	 * @param: @param compay
	 * @param: @return
	 * @return 
	 * @throws
	 * @author zzx
	 * @Date 2018年8月15日
	 */
	public Map<String,Object> queryProductList(Map<String,String> map){
		List<Map<String,String>> list = productMapper.queryProductList(map);
		int count = productMapper.queryProductListCount(map);
		Map<String,Object> bean = new HashMap<>();
		bean.put("total", Integer.toString(count));
		Map<String,Object> map1 = new HashMap<>();
		map1.put("beans", list);
		map1.put("bean", bean);
		map1.put("busiCode", "");
    	map1.put("returnCode", "0");
    	map1.put("returnMessage", "");
    	map1.put("object", "");
    	map1.put("busiCode", ""); 
		return map1;
	}
	/**
	 * 
	 * @Title  产品介绍详情
	 * @Description:  queryProductDtlById
	 * @param: @param product
	 * @param: @return
	 * @return 
	 * @throws
	 * @author zzx
	 * @Date 2018年8月15日
    */
	public Map<String,Object> queryProductDtlById(Product product) throws Exception{
		product.setValidStsCd("1");
		Map<String,String> bean = productMapper.queryProductDtlById(product);
		Map<String,Object> map1 = new HashMap<>();
		map1.put("beans", null);
		map1.put("bean", bean);
		map1.put("busiCode", "");
    	map1.put("returnCode", "0");
    	map1.put("returnMessage", "");
    	map1.put("object", "");
    	map1.put("busiCode", ""); 
		return map1;		
	}
	/**
	 * 
	 * @Title  产品介绍新增
	 * @Description:  saveProductInfo
	 * @param: @param product
	 * @param: @return
	 * @return 
	 * @throws
	 * @author zzx
	 * @Date 2018年8月15日
 */
	public Map<String,Object> saveProductInfo(Product product) throws Exception{
		String productId = getSequence("t_pt_product");		
		product.setProductId(productId);
		product.setValidStsCd("1");
		int flag = productMapper.saveProductInfo(product);
		String filePaths = product.getFilePaths();
    	String crtUserId = product.getCrtUserId();
    	if(StringUtil.isNotEmpty(filePaths)){
    		String arr[] = filePaths.split(",");
    		for(int i=0;i<arr.length;i++){
    			Map<String,String> map = new HashMap<>();
    			String fileId = getSequence("t_pt_product_file");	
    			map.put("fileId", fileId);
    			map.put("productId", productId);
    			map.put("fileTypeCd", "01");
    			map.put("fielNm", arr[i]);
    			map.put("filePath", arr[i]);
    			map.put("crtUserId", crtUserId);
    			map.put("validStsCd", "1");
    			productMapper.saveProductInfoFile(map);
    		}
    	}
    	Map<String,Object> bean = new HashMap<>();
    	if(flag != 1){
    		bean.put("rtnCd", "9999");
    	}else {
    		bean.put("rtnCd", "00");
    	}
    	Map<String,Object> map1 = new HashMap<>();
		map1.put("bean", bean);
		map1.put("busiCode", "");
    	map1.put("returnCode", "0");
    	map1.put("returnMessage", "");
    	map1.put("object", "");
    	map1.put("busiCode", "");		
    	return map1;
	}
	/**
		 * 
		 * @Title  产品介绍修改
		 * @Description:  updateProductInfo
		 * @param: @param product
		 * @param: @return
		 * @return 
		 * @throws
		 * @author zzx
		 * @Date 2018年8月15日
	 */
	public Map<String,Object> updateProductInfo(Product product) throws Exception{
		String productId = product.getProductId();		
		int flag = productMapper.updateProductInfo(product);
		String filePaths = product.getFilePaths();
    	String crtUserId = product.getModfUserId();
    	Map<String,String> map1 = new HashMap<>();
    	map1.put("productId", productId);
    	productMapper.delProductInfoFile(map1);
    	if(StringUtil.isNotEmpty(filePaths)){
    		String arr[] = filePaths.split(",");
    		for(int i=0;i<arr.length;i++){
    			Map<String,String> map = new HashMap<>();
    			String fileId = getSequence("t_pt_product_file");	
    			map.put("fileId", fileId);
    			map.put("productId", productId);
    			map.put("fileTypeCd", "01");
    			map.put("fielNm", arr[i]);
    			map.put("filePath", arr[i]);
    			map.put("crtUserId", crtUserId);
    			map.put("validStsCd", "1");
    			productMapper.saveProductInfoFile(map);
    		}
    	}
    	Map<String,Object> bean = new HashMap<>();
    	if(flag != 1){
    		bean.put("rtnCd", "9999");
    	}else {
    		bean.put("rtnCd", "00");
    	}
    	Map<String,Object> map2 = new HashMap<>();
		map2.put("bean", bean);
		map2.put("busiCode", "");
    	map2.put("returnCode", "0");
    	map2.put("returnMessage", "");
    	map2.put("object", "");
    	map2.put("busiCode", "");		
    	return map2;
	}
	/**
	 * 
	 * @Title  产品介绍删除
	 * @Description:  delProductInfo
	 * @param: (Map<String,String>
	 * @param: @return
	 * @return 
	 * @throws
	 * @author zzx
	 * @Date 2018年8月15日
	 */
	public Map<String,Object> delProductInfo(Map<String,String> map) throws Exception{
		productMapper.delProductInfoFile(map);
		int flag = productMapper.delProductInfo(map);
		Map<String,Object> bean = new HashMap<>();
    	if(flag != 1){
    		bean.put("rtnCd", "9999");
    	}else {
    		bean.put("rtnCd", "00");
    	}
    	Map<String,Object> map1 = new HashMap<>();
		map1.put("bean", bean);
		map1.put("busiCode", "");
    	map1.put("returnCode", "0");
    	map1.put("returnMessage", "");
    	map1.put("object", "");
    	map1.put("busiCode", "");		
    	return map1;
	}
}
